package com.itheima.ai.config;


import com.itheima.ai.constants.SystemConstants;
import com.itheima.ai.tools.CourseTools;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.memory.repository.jdbc.JdbcChatMemoryRepository;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import static com.itheima.ai.constants.SystemConstants.SERVICE_SYSTEM_PROMPT;

@Configuration
public class SpringAIConfiguration {


   // @Primary


    public ChatClient normalChatClient(DeepSeekChatModel chatModel, ChatMemory chatMemory){
        return ChatClient.builder(chatModel)// 创建ChatClient工厂
                .defaultOptions(ChatOptions.builder().model("deepseek-reasoner").build())//推理模式
                .defaultSystem("你是可爱又迷人的反派角色，火箭队的武藏")
                .defaultAdvisors(
                        SimpleLoggerAdvisor.builder().build(), // 添加日志记录advisor
                        MessageChatMemoryAdvisor.builder(chatMemory).build() // 会话记忆advisor
                )
                .build(); // 构建ChatClient实例

    }

    @Bean
    public ChatMemory chatMemory(JdbcChatMemoryRepository chatMemoryRepository) {
        return MessageWindowChatMemory.builder()
                .chatMemoryRepository(chatMemoryRepository)
                .maxMessages(20)
                .build();
    }


    //保存在内存中
    @Bean
    public ChatMemory inMemoryChatMemory() {
        return MessageWindowChatMemory.builder()
                .chatMemoryRepository(new InMemoryChatMemoryRepository())
                .maxMessages(20)
                .build();
    }


    //哄哄模拟器
   // @Primary  //解决bean 冲突的问题
    public ChatClient gameChatClient(
            DeepSeekChatModel chatModel, ChatMemory inMemoryChatMemory){
        return ChatClient.builder(chatModel)
                .defaultOptions(ChatOptions.builder().model("deepseek-reasoner").build()) //使用的是deepseek 擅长推理
                .defaultSystem(SystemConstants.GAME_SYSTEM_PROMPT)
                .defaultAdvisors(
                        SimpleLoggerAdvisor.builder().build(),
                        MessageChatMemoryAdvisor.builder(inMemoryChatMemory).build()
                )
                .build();
    }


    //课程客服
    @Primary
    @Bean
    public ChatClient serviceChatClient(
            DeepSeekChatModel chatModel,
            ChatMemory chatMemory,
            CourseTools courseTools) {
        return ChatClient.builder(chatModel)
                .defaultSystem(SERVICE_SYSTEM_PROMPT)
                .defaultAdvisors(
                        SimpleLoggerAdvisor.builder().build(),
                        MessageChatMemoryAdvisor.builder(chatMemory).build()
                )
                .defaultTools(courseTools)
                .build();
    }

}
